package com.fancyy.scm.base;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableBoolean;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fancyy.scm.BRApplication;
import com.fancyy.scm.base.annotation.Layout;
import com.fancyy.scm.util.LogUtil;
import com.fancyy.scm.util.ToastUtil;

import java.lang.reflect.Field;

/**
 * Created by any on 17/6/19.
 */

public class BaseFragment<DataBinding extends ViewDataBinding> extends Fragment {

    public ObservableBoolean isRefreshing = new ObservableBoolean(false);

    public SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            doOnRefresh();
        }
    };

    public void doLoadMore() {

    }

    public void doOnRefresh() {

    }

    DataBinding dataBinding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Layout layout = getClass().getAnnotation(Layout.class);
        if (layout != null) {
            dataBinding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), layout.value(), container, false);
//            TextView titleView = (TextView) dataBinding.getRoot().findViewById(R.id.actionbarTitle);
//            if (titleView != null) {
//                titleView.setText(layout.title());
//            }
//            TextView rightButtonTextView = (TextView)dataBinding.getRoot().findViewById(R.id.textButton);
//            if (rightButtonTextView != null) {
//                rightButtonTextView.setText(layout.rightButton());
//                rightButtonTextView.setOnClickListener(v -> onRightButtonClick());
//            }
            bindViewModelToUI();
            return dataBinding.getRoot();
        } else {
            LogUtil.w("DataBinding警告->需要使用数据绑定功能必须使用@Layout注解注入布局");
            return null;
        }
    }

    public void onRightButtonClick() {

    }

    private void bindViewModelToUI() {
        if (dataBinding != null) {
            // 绑定两者
            Field[] declaredFields = dataBinding.getClass().getDeclaredFields();
            for (Field declaredField : declaredFields) {
                if (declaredField.getType().equals(getClass())) {
                    declaredField.setAccessible(true);
                    try {
                        declaredField.set(dataBinding, this);
                    } catch (IllegalAccessException e) {
                    }
                    declaredField.setAccessible(false);
                    break;
                }
            }
        }
    }

    public BaseFragmentActivity getSupportedActivity() {
        return (BaseFragmentActivity) getActivity();
    }

    public DataBinding getDataBinding() {
        return dataBinding;
    }

    /**
     * 弹出一个提示信息框
     *
     * @param message
     */
    public void showToast(String message) {
        ToastUtil.showToast(BRApplication.getInstance(), message);
    }

    public boolean isStop = false;
    @Override
    public void onStop() {
        super.onStop();
        isStop = true;
        Log.d("XXX", getClass().getSimpleName() + " stop");
    }

    @Override
    public void onResume() {
        super.onResume();
        isStop = false;
        Log.d("XXX", getClass().getSimpleName() + " start");
    }

    /**
     * 打开一个页面
     *
     * @param activityClass
     */
    public void startActivity(Class<? extends Activity> activityClass) {
        startActivity(activityClass, false);
    }

    /**
     * 打开一个页面
     * 并且设置成是否关闭当前页面
     *
     * @param activityClass
     * @param finishSelf
     */
    public void startActivity(Class<? extends Activity> activityClass, boolean finishSelf) {
        Intent intent = new Intent(getActivity(), activityClass);
        startActivity(intent);
        if (finishSelf) {
            getActivity().finish();
        }
    }
}
